package com.xiaocikuai.smallchunks.service.Card.impl;

import com.xiaocikuai.smallchunks.entity.Card.CardChunks;
import com.xiaocikuai.smallchunks.mapper.Card.CardChunksMapper;
import com.xiaocikuai.smallchunks.service.Card.ICardChunksService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 微语境单词卡 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-09-14
 */
@Service
public class CardChunksServiceImpl extends ServiceImpl<CardChunksMapper, CardChunks> implements ICardChunksService {

    @Resource
    private CardChunksMapper cardChunksMapper;

    public boolean collectionAdd(long uid, int cid, LocalDateTime time){
        return cardChunksMapper.collectionAdd(cid, uid, time );
    }

    public boolean collectionDel(long uid, int cid){
        return cardChunksMapper.collectionDel(cid, uid);
    }


    public List<CardChunks> collectList(int pid, Integer uid) {
        List<CardChunks> list = new ArrayList<>();
        Set<Integer> cids = cardChunksMapper.collectList(uid);
        if (cids == null || cids.isEmpty()) {
            return new ArrayList<>();
        }
        cids.forEach(cid -> {
            if (cardChunksMapper.getCardCollect(cid, pid) != null) {
                CardChunks chunks=cardChunksMapper.getCardCollect(cid, pid);
                chunks.setIsCollect(cardChunksMapper.isCollect(cid,uid));
                list.add(chunks);
            }
        });
        return list;

    }

}
